package com.example.administrator.exam;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.util.LruCache;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/12/30.
 */
public class NewActivity extends AppCompatActivity {
    private static RequestQueue mRequestQueue;
    ImageView iv_tu;
    TextView tv_yongh;
//    private RequestQueue mRequestQueue;
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newactivity);
        iv_tu = (ImageView) findViewById(R.id.iv_tu);
        tv_yongh = (TextView) findViewById(R.id.tv_yongh);
        denglu();
    }

    public void denglu(){
        Intent intent = getIntent();
        String user_id = intent.getStringExtra("user_id");
        String ssn_id = intent.getStringExtra("ssn_id");
        mRequestQueue =  Volley.newRequestQueue(NewActivity.this);
        //   "user_id": "PKUS70000671",
        //"ssn_id": "s5itu97070i56h775b8h",
        //http://api.gzkbapp.com/getUserInfo.action?userId=PKUS70000671&ssnId=s5itu97070i56h775b8h
        String path = "http://api.gzkbapp.com/getUserInfo.action?userId="+user_id+"&ssnId="+ssn_id;
        StringRequest stringRequest = new StringRequest(path, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONObject user_info = jsonObject.optJSONObject("user_info");
                    String rtn_cd = jsonObject.optString("rtn_cd");
                    String ava_url = user_info.optString("ava_url");
                    String user_urd_cnt = user_info.optString("user_urd_cnt");
                    String user_name = user_info.optString("user_name");
                    String acrdt_cnt = user_info.optString("acrdt_cnt");
                    String crdt_cnt = user_info.optString("crdt_cnt");
                    String user_nknm = user_info.optString("user_nknm");
                    String user_sexy = user_info.optString("user_sexy");
                    String dst_flg = user_info.optString("dst_flg");
                    String user_lvl = user_info.optString("user_lvl");
                    NewActivity.setImageLoader(ava_url,iv_tu);
                    tv_yongh.setText(user_nknm);
                    Toast.makeText(getApplicationContext(),user_nknm,Toast.LENGTH_LONG).show();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(NewActivity.this,"数据异常",Toast.LENGTH_LONG).show();
            }
        });
        mRequestQueue.add(stringRequest);

    }
    public static void setImageLoader(String url, ImageView imageView) {
        ImageLoader loader = new ImageLoader(mRequestQueue, new BitmapCache());
        ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(imageView, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
        loader.get(url, imageListener);
    }

}

class BitmapCache implements ImageLoader.ImageCache {
    private LruCache<String, Bitmap> mMemoryCache;
    public BitmapCache() {
        // Use 1/16th of the available memory for this memory cache.
        final int cacheSize = 1024 * 1024 *  16;
        mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getRowBytes() * value.getHeight();
            }
        };
    }
    @Override
    public Bitmap getBitmap(String url) {
        return mMemoryCache.get(url);
    }
    @Override
    public void putBitmap(String url, Bitmap bitmap) {
        mMemoryCache.put(url, bitmap);
    }
}
